//
//  createNewSceneView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/6/19.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "createNewSceneView.h"
#import "myCollectionViewCell.h"

@interface createNewSceneView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    NSIndexPath *firstIndexPath;
}

@property (nonatomic, strong) UICollectionView *myCollectionView;

@end

@implementation createNewSceneView

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = BACKGROUND_COLOR;
        self.imageName = @"Image/scene/1";
        [self addLabel];
        [self addSubview:self.myTextField];
        [self addSubview:self.myCollectionView];
    }
    return self;
}

- (void)addLabel
{
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.bounds.size.width, 30)];
    nameLabel.text = @"场景名称:";
    nameLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:nameLabel];
    
    UILabel *imageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, self.bounds.size.width, 30)];
    imageLabel.text = @"选择图像:";
    imageLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:imageLabel];
}

#pragma mark - UICollectionViewDataSource 和 UICollectionViewDelegate协议
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    myCollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if(Cell == nil){
        
    }
    
    NSString *imageName = [NSString stringWithFormat:@"Image/scene/%d",(int)indexPath.row + 1];
    [Cell setNewSceneImage:GET_IAMGE_WITH_NAME(imageName)];
    [Cell addSubview:Cell.imageView];
    Cell.layer.borderColor = [UIColor whiteColor].CGColor;
    Cell.layer.borderWidth = 2;
    Cell.layer.cornerRadius = 5.0;
    
    if(indexPath.row == 0){
        firstIndexPath = indexPath;
        Cell.layer.borderColor = TEXT_GREEN_COLOR.CGColor;
    }
    
    return Cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row != 0){
        myCollectionViewCell *firstCell = (myCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:firstIndexPath];
        firstCell.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    myCollectionViewCell *cell = (myCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:indexPath];
    cell.layer.borderColor = TEXT_GREEN_COLOR.CGColor;
    self.imageName = [NSString stringWithFormat:@"Image/scene/%d",(int)indexPath.row + 1];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    myCollectionViewCell *cell = (myCollectionViewCell *)[self.myCollectionView cellForItemAtIndexPath:indexPath];
    cell.layer.borderColor = [UIColor whiteColor].CGColor;
}

#pragma mark - Setter Getter
- (UITextField *)myTextField
{
    if(!_myTextField){
        _myTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 45, self.bounds.size.width - 20, 35)];
        _myTextField.backgroundColor = [UIColor whiteColor];
        _myTextField.textColor = [UIColor blackColor];
        _myTextField.keyboardType = UIKeyboardTypeDefault;
        _myTextField.returnKeyType = UIReturnKeyDone;  //返回键类型是Done
        _myTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;  //手写字母大写 关闭
        _myTextField.autocorrectionType = UITextAutocorrectionTypeNo;  //自动改正 关闭
        _myTextField.clearButtonMode = UITextFieldViewModeWhileEditing;   //最后面添加一个 ×
        _myTextField.secureTextEntry = NO;   //非密文
        _myTextField.placeholder = @"请输入场景名称";
        _myTextField.layer.cornerRadius = 5.0;
        _myTextField.layer.masksToBounds = YES;
    }
    return _myTextField;
}

- (UICollectionView *)myCollectionView
{
    if(!_myCollectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(90, 90);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.minimumInteritemSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 125,self.bounds.size.width, self.bounds.size.height - 125)
                                               collectionViewLayout:layout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        
        _myCollectionView.backgroundColor = [UIColor whiteColor];
        [_myCollectionView registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    }
    return _myCollectionView;
}


@end
